//
//  HistoryWabController.m
//  QRBuildScan
//
//  Created by gongjie on 12-1-3.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "HistoryWabController.h"
#import "MainTabbarController.h"
#import "DetailsContorller.h"
#import "QRCodeData.h"
@implementation HistoryWabController
@synthesize hw_leftbuttn;
@synthesize hw_rightbuttn;
@synthesize hw_QRCodeData;

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
-(id)init{
    if( self = [super init])
    {
        hw_rightbuttn= [UIButton buttonWithType:UIButtonTypeCustom];
        hw_rightbuttn.frame = CGRectMake(270,10, 25, 25);
        [hw_rightbuttn setBackgroundImage:[UIImage imageNamed:@"beizhi.png"] forState:UIControlStateNormal];
        [hw_rightbuttn addTarget:self action:@selector(changhwright) forControlEvents:UIControlEventTouchUpInside];
        [hw_rightbuttn retain];
        
        hw_leftbuttn = [UIButton buttonWithType:UIButtonTypeCustom];
        hw_leftbuttn.frame = CGRectMake(10,7, 60, 30);
        [hw_leftbuttn setBackgroundImage:[UIImage imageNamed:@"cancel.png"] forState:UIControlStateNormal];
        [hw_leftbuttn addTarget:self action:@selector(changhwleft) forControlEvents:UIControlEventTouchUpInside];
        [hw_leftbuttn retain];
        hw_url = nil;
    }
    return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [self Createfunction];
    hw_webview= [[UIWebView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height-50)];
    hw_webview.backgroundColor = [UIColor clearColor];
    hw_webview.scalesPageToFit = YES;
    hw_webview.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    hw_webview.delegate = self;
    
    hw_ActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    hw_ActivityIndicator.frame = CGRectMake(hw_webview.frame.size.width/2-15,(hw_webview.frame.size.height-60)/2-15,30,30);
    [hw_webview addSubview:hw_ActivityIndicator];
    
    [self.view addSubview:hw_webview];

    [self sendurl:hw_QRCodeData.content];
}

-(void)Createfunction
{
    hw_function = [[UIView alloc] initWithFrame:CGRectMake(0, 390, 320, 50)];
    [hw_function setBackgroundColor:[UIColor blackColor]];
    
    
    hw_qian =[UIButton buttonWithType:UIButtonTypeCustom];
    hw_qian.frame = CGRectMake(25, 10, 25, 25);
    [hw_qian setBackgroundImage:[UIImage imageNamed:@"qian1.png"] forState:UIControlStateNormal];
    [hw_qian addTarget:self action:@selector(changeLeftButton) forControlEvents:UIControlEventTouchUpInside];
    [hw_function addSubview:hw_qian];
    
    hw_hou =[UIButton buttonWithType:UIButtonTypeCustom];
    hw_hou.frame = CGRectMake(90, 10, 25, 25);
    [hw_hou setBackgroundImage:[UIImage imageNamed:@"hou1.png"] forState:UIControlStateNormal];
    [hw_hou addTarget:self action:@selector(changeRightButton) forControlEvents:UIControlEventTouchUpInside];
    [hw_function addSubview:hw_hou];
    
    hw_shua = [UIButton buttonWithType:UIButtonTypeCustom];
    hw_shua.frame = CGRectMake(150, 10, 25, 25);
    [hw_shua setImage:[UIImage imageNamed:@"shua1.png"] forState:UIControlStateNormal];
	[hw_shua setImage:[UIImage imageNamed:@"shua.png"] forState:UIControlStateHighlighted];
    [hw_shua addTarget:self action:@selector(changhRefresh) forControlEvents:UIControlEventTouchUpInside];
    [hw_function addSubview:hw_shua];
    
    hw_xin = [UIButton buttonWithType:UIButtonTypeCustom];
    hw_xin.frame = CGRectMake(210, 10, 30, 25);
    [hw_xin setImage:[UIImage imageNamed:@"xin2.png"] forState:UIControlStateNormal];
	[hw_xin setImage:[UIImage imageNamed:@"xin1.png"] forState:UIControlStateHighlighted];
    [hw_xin addTarget:self action:@selector(changhInformation) forControlEvents:UIControlEventTouchUpInside];
    [hw_function addSubview:hw_xin];
    
    hw_wab = [UIButton buttonWithType:UIButtonTypeCustom];
    hw_wab.frame = CGRectMake(270, 10, 25, 25);
    [hw_wab setImage:[UIImage imageNamed:@"tiao2.png"] forState:UIControlStateNormal];
	[hw_wab setImage:[UIImage imageNamed:@"tiao1.png"] forState:UIControlStateHighlighted];
    [hw_wab addTarget:self action:@selector(changhJump) forControlEvents:UIControlEventTouchUpInside];
    [hw_function addSubview:hw_wab];
    
    [self.view addSubview:hw_function];

}
-(void)changhwleft
{
    if (hw_leftbuttn.superview) {
        [hw_leftbuttn removeFromSuperview];
    }
    if (hw_rightbuttn.superview) {
        [hw_rightbuttn removeFromSuperview];
    }
    [MainTabbarController setTabbarState:NO];
    self.hidesBottomBarWhenPushed= NO;
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)changhwright
{
    if (hw_leftbuttn.superview) {
        [hw_leftbuttn removeFromSuperview];
    }
    if (hw_rightbuttn.superview) {
        [hw_rightbuttn removeFromSuperview];
    }
    DetailsContorller* iDetails = [[DetailsContorller alloc] init];
    iDetails.D_QRCodeData = hw_QRCodeData;
    [self.navigationController pushViewController:iDetails animated:YES];
    [iDetails release];
}
-(void)sendurl:(NSString*)url
{	
	NSURL* comurl =[NSURL URLWithString:url];
    [hw_webview loadRequest:[NSURLRequest requestWithURL:comurl]];
    [hw_ActivityIndicator startAnimating];
}

-(void) changeLeftButton
{
	[hw_webview goBack];
	
}
-(void) changeRightButton
{
	[hw_webview goForward];
}
-(void)changhRefresh
{
    if (hw_url!=nil&&hw_url!=@"") {
        [self sendurl:hw_url];
    }

}
-(void)changhInformation
{
    
}
-(void)changhJump
{
    
	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@""
															 delegate:self
													cancelButtonTitle:@"取消" 
											   destructiveButtonTitle:nil
													otherButtonTitles:@"重Safari中打开网址", nil];
	actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
	actionSheet.destructiveButtonIndex = 1;	// make the second button red (destructive)
	
	actionSheet.tag = 9988;
	[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
	[actionSheet release];
}
#pragma mark UIWebViewDelegate
//- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
//{
//	
//}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
        [hw_ActivityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{

	NSURLRequest* weburl = [webView request];
	hw_url= [[weburl URL] absoluteString];
    [hw_url retain];
    [hw_ActivityIndicator stopAnimating];
	if (webView.canGoBack) {
		[hw_qian setImage:[UIImage imageNamed:@"qian2.png"] forState:UIControlStateNormal];
	}else {
		[hw_qian setImage:[UIImage imageNamed:@"qian1.png"] forState:UIControlStateNormal];
	}
	if (webView.canGoForward) {
		[hw_hou setImage:[UIImage imageNamed:@"hou2.png"] forState:UIControlStateNormal];
	}else {
		[hw_hou setImage:[UIImage imageNamed:@"hou1.png"] forState:UIControlStateNormal];
	}
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 9988&&buttonIndex == 0) {
        if (hw_url!=nil&&hw_url!=@"") 
        {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:hw_url]];
        }
    }


}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)dealloc
{
    [hw_webview release];
    [hw_function release];
    [hw_ActivityIndicator release];
    [hw_url release];
    hw_qian = nil;
    hw_hou = nil;
    hw_shua = nil;
    hw_xin = nil;
    hw_wab = nil;
    hw_rightbuttn = nil;
    hw_leftbuttn = nil;
    [super dealloc];
}
@end
